package com.healthcare;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import webservices.HealthCareServerProxy;

/**
 * Servlet implementation class Article
 */
public class Article extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Article() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String prodName = request.getParameter("data");
	String docid =	request.getSession().getAttribute("docid").toString();
		System.out.println("docid inside Article"+docid);
		String spec = request.getParameter("spec");
	
		int postId = Integer.parseInt(prodName);
		int dcid = Integer.parseInt(docid);
		//Boolean postStatus = false;
		String status = "false";
			
				HealthCareServerProxy proxy = new HealthCareServerProxy();
				if(proxy.deleteArticles(dcid,postId));
				{
				RequestDispatcher dispatcher = request.getRequestDispatcher("/PostArticle.jsp");
				System.out.println("inside the delet");
				status="done";
				request.setAttribute("status", status);
				
				dispatcher.forward(request, response);
				System.out.println("inside the delet11");
				}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	String 	inputlabel = request.getParameter("label");
	String	article = request.getParameter("article");
	String	tags = request.getParameter("tags");
		System.out.println("username in post" + "M");
		DateFormat dateFormat = new SimpleDateFormat(
				"EEE, d MMM yyyy HH:mm:ss Z");
		Date date = new Date();
String specialization = request.getParameter("specialization");
System.out.println("specialization" +specialization);
		String docid  = request.getSession().getAttribute("docid").toString();
		HealthCareServerProxy proxy = new HealthCareServerProxy();

		String status = proxy.postArticle(inputlabel, article,
				docid, tags,dateFormat.format(date));
		System.out.println(status);
		String staticStatus = "Post is added!!";
		request.setAttribute("postStatus", status);
		if (status.equals(staticStatus)) {
		
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/DocArticles.jsp");

			dispatcher.forward(request, response);
			System.out.println("after the insert");
		} else {

			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/PostArticle.jsp");

			
			dispatcher.forward(request, response);
			System.out.println("after no insert");
		}

		
	}

}
